<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="companyUscc">
        <el-input
          v-model="queryParams.companyUscc"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工商注册号" prop="companyBrn">
        <el-input
          v-model="queryParams.companyBrn"
          placeholder="请输入工商注册号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营状态" prop="companyOpstatus">
        <el-select v-model="queryParams.companyOpstatus" placeholder="请选择经营状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_company_opstatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业类型" prop="companyComptype">
        <el-input
          v-model="queryParams.companyComptype"
          placeholder="请输入企业类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营范围" prop="companyBusscope">
        <el-input
          v-model="queryParams.companyBusscope"
          placeholder="请输入经营范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属行业" prop="companyIndsector">
        <el-input
          v-model="queryParams.companyIndsector"
          placeholder="请输入所属行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法定代表人" prop="companyLegalrep">
        <el-input
          v-model="queryParams.companyLegalrep"
          placeholder="请输入法定代表人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册资本" prop="companyRegcap">
        <el-input
          v-model="queryParams.companyRegcap"
          placeholder="请输入注册资本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成立日期" prop="companyEstdate">
        <el-input
          v-model="queryParams.companyEstdate"
          placeholder="请输入成立日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营期限" prop="companyOpperiod">
        <el-input
          v-model="queryParams.companyOpperiod"
          placeholder="请输入经营期限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记机关" prop="companyRegauth">
        <el-input
          v-model="queryParams.companyRegauth"
          placeholder="请输入登记机关"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册地址" prop="companyRegaddr">
        <el-input
          v-model="queryParams.companyRegaddr"
          placeholder="请输入注册地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:register_company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:register_company:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:register_company:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:register_company:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-agree"
          size="mini"
          :disabled="single"
          @click="handleAgree"
          v-hasPermi="['system:register_company:agree']"
        >批准</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-refuse"
          size="mini"
          :disabled="single"
          @click="handleRefuse"
          v-hasPermi="['system:register_company:refuse']"
        >拒绝</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="register_companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司ID" align="center" prop="registerCompanyId" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="统一社会信用代码" align="center" prop="companyUscc" />
      <el-table-column label="工商注册号" align="center" prop="companyBrn" />
      <el-table-column label="经营状态" align="center" prop="companyOpstatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_company_opstatus" :value="scope.row.companyOpstatus"/>
        </template>
      </el-table-column>
      <el-table-column label="企业类型" align="center" prop="companyComptype" />
      <el-table-column label="经营范围" align="center" prop="companyBusscope" />
      <el-table-column label="所属行业" align="center" prop="companyIndsector" />
      <el-table-column label="法定代表人" align="center" prop="companyLegalrep" />
      <el-table-column label="注册资本" align="center" prop="companyRegcap" />
      <el-table-column label="成立日期" align="center" prop="companyEstdate" />
      <el-table-column label="经营期限" align="center" prop="companyOpperiod" />
      <el-table-column label="登记机关" align="center" prop="companyRegauth" />
      <el-table-column label="注册地址" align="center" prop="companyRegaddr" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:register_company:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:register_company:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改注册公司信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="companyUscc">
          <el-input v-model="form.companyUscc" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="工商注册号" prop="companyBrn">
          <el-input v-model="form.companyBrn" placeholder="请输入工商注册号" />
        </el-form-item>
        <el-form-item label="经营状态" prop="companyOpstatus">
          <el-radio-group v-model="form.companyOpstatus">
            <el-radio
              v-for="dict in dict.type.sys_company_opstatus"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业类型" prop="companyComptype">
          <el-input v-model="form.companyComptype" placeholder="请输入企业类型" />
        </el-form-item>
        <el-form-item label="经营范围" prop="companyBusscope">
          <el-input v-model="form.companyBusscope" placeholder="请输入经营范围" />
        </el-form-item>
        <el-form-item label="所属行业" prop="companyIndsector">
          <el-input v-model="form.companyIndsector" placeholder="请输入所属行业" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="companyLegalrep">
          <el-input v-model="form.companyLegalrep" placeholder="请输入法定代表人" />
        </el-form-item>
        <el-form-item label="注册资本" prop="companyRegcap">
          <el-input v-model="form.companyRegcap" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="成立日期" prop="companyEstdate">
          <el-input v-model="form.companyEstdate" placeholder="请输入成立日期" />
        </el-form-item>
        <el-form-item label="经营期限" prop="companyOpperiod">
          <el-input v-model="form.companyOpperiod" placeholder="请输入经营期限" />
        </el-form-item>
        <el-form-item label="登记机关" prop="companyRegauth">
          <el-input v-model="form.companyRegauth" placeholder="请输入登记机关" />
        </el-form-item>
        <el-form-item label="注册地址" prop="companyRegaddr">
          <el-input v-model="form.companyRegaddr" placeholder="请输入注册地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegister_company, getRegister_company, delRegister_company, addRegister_company, updateRegister_company } from "@/api/system/register_company";
import { listCompany,  addCompany } from "@/api/system/company";

export default {
  name: "Register_company",
  dicts: ['sys_company_opstatus'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 注册公司信息管理表格数据
      register_companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        companyUscc: null,
        companyBrn: null,
        companyOpstatus: null,
        companyComptype: null,
        companyBusscope: null,
        companyIndsector: null,
        companyLegalrep: null,
        companyRegcap: null,
        companyEstdate: null,
        companyOpperiod: null,
        companyRegauth: null,
        companyRegaddr: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        companyUscc: [
          { required: true, message: "统一社会信用代码不能为空", trigger: "blur" }
        ],
        companyBrn: [
          { required: true, message: "工商注册号不能为空", trigger: "blur" }
        ],
        companyOpstatus: [
          { required: true, message: "经营状态不能为空", trigger: "change" }
        ],
        companyComptype: [
          { required: true, message: "企业类型不能为空", trigger: "blur" }
        ],
        companyBusscope: [
          { required: true, message: "经营范围不能为空", trigger: "blur" }
        ],
        companyIndsector: [
          { required: true, message: "所属行业不能为空", trigger: "blur" }
        ],
        companyLegalrep: [
          { required: true, message: "法定代表人不能为空", trigger: "blur" }
        ],
        companyRegcap: [
          { required: true, message: "注册资本不能为空", trigger: "blur" }
        ],
        companyEstdate: [
          { required: true, message: "成立日期不能为空", trigger: "blur" }
        ],
        companyOpperiod: [
          { required: true, message: "经营期限不能为空", trigger: "blur" }
        ],
        companyRegauth: [
          { required: true, message: "登记机关不能为空", trigger: "blur" }
        ],
        companyRegaddr: [
          { required: true, message: "注册地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询注册公司信息管理列表 */
    getList() {
      this.loading = true;
      listRegister_company(this.queryParams).then(response => {
        this.register_companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        registerCompanyId: null,
        companyName: null,
        companyUscc: null,
        companyBrn: null,
        companyOpstatus: null,
        companyComptype: null,
        companyBusscope: null,
        companyIndsector: null,
        companyLegalrep: null,
        companyRegcap: null,
        companyEstdate: null,
        companyOpperiod: null,
        companyRegauth: null,
        companyRegaddr: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.registerCompanyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加注册公司信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const registerCompanyId = row.registerCompanyId || this.ids
      getRegister_company(registerCompanyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改注册公司信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.registerCompanyId != null) {
            updateRegister_company(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegister_company(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const registerCompanyIds = row.registerCompanyId || this.ids;
      this.$modal.confirm('是否确认删除注册公司信息管理编号为"' + registerCompanyIds + '"的数据项？').then(function() {
        return delRegister_company(registerCompanyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/register_company/export', {
        ...this.queryParams
      }, `register_company_${new Date().getTime()}.xlsx`)
    },
    /** 批准按钮操作 */
    handleAgree(row) {
      const registerCompanyIds = row.registerCompanyId || this.ids;
      const firstElement = registerCompanyIds[0]; //ID 的编号
      const agree_company = this.register_companyList.find(company => company.registerCompanyId === firstElement);
      console.log('%this Content:', 'color: red', agree_company);
      this.$modal.confirm('是否确认批准注册公司信息管理编号为"' + registerCompanyIds + '"的数据项？').then(function() {
        
        addCompany(agree_company);
        return delRegister_company(registerCompanyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("批准成功");
      }).catch(() => {});
    },
    /** 拒绝按钮操作 */
    handleRefuse(row) {
      const registerCompanyIds = row.registerCompanyId || this.ids;
      this.$modal.confirm('是否确认拒绝注册公司信息管理编号为"' + registerCompanyIds+ '"的数据项？').then(function() {
        return delRegister_company(registerCompanyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("拒绝成功");
      }).catch(() => {});
    }
  }
};
</script>
